import tkinter
import random
import time
from turtle import color


class Score():
    def __init__(self, canvas, color):
        self.score = 0
        self.canvas = canvas
        self.id = canvas.create_text(450, 50, text=self.score, fill=color)
        pass


class Paddle():
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 200, 20, fill=color)
        canvas.move(self.id, 170, 350)
        self.x = 0
        self.y = 0
        self.canvas.bind_all('<KeyPress-Left>', self.turnLeft)
        self.canvas.bind_all('<KeyPress-Right>', self.turnRight)
        tk.update()

    def move(self):
        self.pos = self.canvas.coords(self.id)
        self.canvas.move(self.id, self.x, self.y)
        self.x = 0
        self.y = 0

    def turnLeft(self, evt):
        self.x = -15

    def turnRight(self, evt):
        self.x = 15


class Ball():
    def __init__(self, canvas, color):
        self.level = [-7, -5, -3, -1, 0, 1, 3, 5, 7]
        self.canvas = canvas
        self.id = canvas.create_oval(240, 240, 260, 260, fill=color)
        # canvas.move(self.id, 100, 100)
        self.x = -3
        self.y = random.choice(self.level)

        pass

    def move(self):
        self.canvas.move(self.id, self.x, self.y)
        self.pos = self.canvas.coords(self.id)
        if (self.pos[1] <= 0):
            self.x = random.choice(self.level)
            self.y = 3
        if (self.pos[0] <= 0):
            self.y = random.choice(self.level)
            self.x = 3
        if (self.pos[2] >= 500):
            self.y = random.choice(self.level)
            self.x = -3
        if (self.pos[3] >= 400):
            self.x = random.choice(self.level)
            self.y = -3
        tk.update()


tk = tkinter.Tk()
tk.title('ball')
tk.resizable(0, 0)
tk.wm_attributes('-topmost', 1)

canvas = tkinter.Canvas(tk, width=500, height=400, bg='black')
canvas.pack()

ball = Ball(canvas, 'red')
paddle = Paddle(canvas, 'green')
score = Score(canvas, 'green')


while True:
    ball.move()
    paddle.move()
    time.sleep(0.01)

tk.update()

tk.mainloop()
